﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Mocola.BLL;
using Mocola.Model;

namespace Mocola.Net.Web
{
    public partial class userlist : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }

        private void BindData() 
        {
            string sql = @"SELECT MU.UserID,MU.UserName,(CASE MU.UserSex WHEN 0 THEN '男' ELSE '女' END) AS Sex ,
                          (CASE MU.Status WHEN 0 THEN '激活' ELSE '锁定' END) AS Status,MU.Email,MU.FullName,
                          (SELECT RoleName FROM Moc_Roles MR WHERE MR.ID=MU.RoleID) AS RoleName FROM Moc_Users MU";
            DataSet dtUsers = Databases.MocolaNet.ExecuteDataSet(CommandType.Text, sql);
            rptUsers.DataSource = dtUsers;
            rptUsers.DataBind();
        }

        protected void rptUsers_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            HiddenField hfUserID = e.Item.FindControl("hfUserID") as HiddenField;
            hfUserID.Value = DataBinder.Eval(e.Item.DataItem, "UserID").ToString();

            // BEGIN: 获取部门信息列表
            Literal lblDepart = e.Item.FindControl("lblDepart") as Literal;
            //string DepartID = lblDepart.Text.Trim();
            string DepartList = string.Empty;

            string sql = "SELECT [Name] FROM Moc_Departments WHERE [ID] in (SELECT DepartID FROM Moc_DepartmentUser WHERE UserID = {0})";
            sql = string.Format(sql, hfUserID.Value);
            IDataReader reader = Databases.MocolaNet.ExecuteReader(CommandType.Text, sql);
            while (reader.Read())
            {
                DepartList += reader[0].ToString() + ";";
            }
            lblDepart.Text = DepartList;
            // END: 获取部门信息列表
        }
    }
}
